package de.pitkley.updater;


import java.io.File;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author pit
 */
public class Util {

    public static File getWorkingDirectory(String applicationName) {
        String userHome = System.getProperty("user.home", ".");
        File workingDirectory;

        switch (getPlatform().ordinal() + 1) {
            case 1:
            case 2:
                workingDirectory = new File(userHome, '.' + applicationName + '/');
                break;
            case 3:
                String applicationData = System.getenv("APPDATA");
                if (applicationData != null) {
                    workingDirectory = new File(applicationData, '.' + applicationName + '/');
                } else {
                    workingDirectory = new File(userHome, '.' + applicationName + '/');
                }
                break;
            case 4:
                workingDirectory = new File(userHome, "Library/Application Support/" + applicationName);
                break;
            default:
                workingDirectory = new File(userHome, applicationName + '/');
        }
        if ((!workingDirectory.exists()) && (!workingDirectory.mkdirs())) {
            throw new RuntimeException("The working directory could not be created: " + workingDirectory);
        }
        return workingDirectory;
    }

    private static OS getPlatform() {
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("win")) {
            return OS.windows;
        } else if (osName.contains("mac")) {
            return OS.macos;
        } else if (osName.contains("solaris") || osName.contains("sunos")) {
            return OS.solaris;
        } else if (osName.contains("linux") || osName.contains("unix")) {
            return OS.linux;
        } else {
            return OS.unknown;
        }
    }

    private static enum OS {

        linux, solaris, windows, macos, unknown;
    }
}
